How to Develop an Online Shopping Cart (Part Three)
Table of Contents
ProcessOrder.cfm
On the ProcessOrder.cfm page, the shopping cart processes the order and gives the user a confirmation message. Open ProcessOrder.cfm in the Code view. Notice a comment on lines 29 through 37 that says this is where you would put the code to authorize the shopper's credit card data. There are a couple of good real-time gateway custom tags for Authorize Net and Verisign PayFlo Pro on the Macromedia ColdFusion Exchange that would work nicely here. You could look at using PayPal as well, although since PayPal is not a real-time processor and requires a jump to their servers to complete the transaction, you would need to modify your app.
Writing Data to the Database
Next, assuming the payment verified, the application writes the order
to the database. First, the application adds the customer using the
following cfquery database insert code.
<cfquery datasource="DevNetExample">
INSERT INTO customers (custName, custAddress, custCity, custState, custZip)
VALUES ('#FORM.custName#', '#FORM.custAddress#', '#FORM.custCity#', '#FORM.custState#', '#FORM.custZip#')
</cfquery>
Then, the application retrieves the customer ID it just created in the above cfquery insert and uses the customer ID when it adds the data to the orders table.
<!--- Insert the Order Total and crate an Order record ---> <cfquery datasource="DevNetExample"> INSERT INTO Orders (orderCustID,orderTotal) VALUES (#getNewCutID.NewCustomerID#, #form.orderTotal#) </cfquery>
Again, the application retrieves the order number it just created and uses the order number when it loops through the cart and adds the order items to the OrfderItems table.
<!--- Get the ID of the Order Record we just created ---> <cfquery name="getNewOrderID" datasource="DevNetExample"> SELECT MAX (orderID) AS NewOrderID FROM Orders </cfquery>
And finally, the application loops through the cart session array and adds the items for this order to the database.
<!--- Now loop through the Cart Array and create a record in the Order Items table relate the foreign Key field to the Orders table. ---> <cfloop from="1" to="#ArrayLen(Session.Cart.ItemID)#" index="ThisItem"> <cfquery datasource="DevNetExample"> INSERT INTO OrderItems (orditmOrderID,orditmItemID,orditmQty) VALUES (#getNewOrderID.NewOrderID#, #Session.Cart.ItemID[ThisItem]#, #Session.Cart.Qty[ThisItem]#) </cfquery> </cfloop>
And that's it! A thank you message displays to the customer.
One last thing to notice on the ProcessOrder.cfm page is the code found on lines 115 through 118.
<!--- //All Done Now ... Need to Kill Session // ---> <cflock timeout="10" throwontimeout="NO" type="EXCLUSIVE" scope="SESSION"> <cfset StructDelete(session, "cart") > </cflock>
This code clears the cart session. Since the customer is finished, you want to delete the cart session just in case the customer hits the back button or goes back into the site to look around some more. We don't want the cart session hanging around and causing possible confusion.